<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Poll
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 * @version    $Id: Core.php 7443 2010-09-22 07:25:41Z john $
 * @author     Steve
 */

/**
 * @category   Application_Extensions
 * @package    Poll
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 */
class Poll_Plugin_Core {
	public function onStatistics($event) {
		$table = Engine_Api::_ ()->getDbTable ( 'polls', 'poll' );
		$select = new Zend_Db_Select ( $table->getAdapter () );
		$select->from ( $table->info ( 'name' ), 'COUNT(*) AS count' );
		$event->addResponse ( $select->query ()->fetchColumn ( 0 ), 'poll' );
		
		$table = Engine_Api::_ ()->getDbTable ( 'votes', 'poll' );
		$select = new Zend_Db_Select ( $table->getAdapter () );
		$select->from ( $table->info ( 'name' ), 'COUNT(*) AS count' );
		$event->addResponse ( $select->query ()->fetchColumn ( 0 ), 'poll vote' );
	}
	
	public function onUserDeleteBefore($event) {
		$payload = $event->getPayload ();
		if ($payload instanceof User_Model_User) {
			// Delete polls
			$pollTable = Engine_Api::_ ()->getDbtable ( 'polls', 'poll' );
			$pollSelect = $pollTable->select ()->where ( 'user_id = ?', $payload->getIdentity () );
			foreach ( $pollTable->fetchAll ( $pollSelect ) as $poll ) {
				$poll->delete ();
			}
		}
	}
}